<?php

!defined('IN_SYSTEM') && exit('Access denied');

class settingControlPage extends commonControlPage {

    public function thumbModule() {
        if (POST && AJAX) {

            $data = getRequests(array('thumb_bw','thumb_bh','thumb_sw','thumb_sh'));
            if(count($data) != 4) {
                $this->outputJson(0,'请补全信息');
            }

            if (!is_numeric($data['thumb_bw']) OR !is_numeric($data['thumb_bh']) OR !is_numeric($data['thumb_sw']) OR !is_numeric($data['thumb_sh'])) {
                $this->outputJson(0,'尺寸必须为数字');
            }

            $_ENV['db']->runSql("REPLACE INTO `setting` (`k`,`v`) VALUES ('thumb_bw','{$data['thumb_bw']}'),('thumb_bh','{$data['thumb_bh']}'),('thumb_sw','{$data['thumb_sw']}'),('thumb_sh','{$data['thumb_sh']}')");
            class_cache::delete('config');
            $this->outputJson(1,'操作成功');
        }

        $rowset = $_ENV['db']->fetchAll("SELECT * FROM `setting` WHERE `k` IN ('thumb_bw','thumb_bh','thumb_sw','thumb_sh')");
        foreach ((array)$rowset as $row) {
            echo $k;
            $this->view->$row['k'] = $row['v'];
        }
        $this->showMainPage('admincp/setting_thumb');
    }
    
    public function systemtypeModule() {
        if (POST && AJAX) {
            $systemtype = htmlspecialchars(str_replace("\r","",trim($_POST['systemtype'])));

            foreach(explode("\n",$systemtype) as $type) {
                if (mb_strlen($type,'UTF-8') > 15) {
                    $this->outputJson(-1,"[{$type}]超过了15字符限制");
                }
            }

            $_ENV['db']->runSql("REPLACE INTO `setting` (`k`,`v`) VALUES ('systemtype','{$systemtype}')");
            class_cache::delete('config');
            $this->outputJson(1,'操作成功');
        }
        
        $this->view->systemtype = $_ENV['config']['systemtype'];
        $this->showMainPage("admincp/setting_systemtype");
    }

    public function systemModule() {
        if (POST && AJAX) {

            foreach ($_POST as $key=>$val) {
                $_ENV['db']->runSql("REPLACE INTO `setting` SET `k` = '{$key}', `v` = '{$val}'");
            }

            class_cache::delete('config');
            $this->outputJson(1,'操作成功');
        }

        $this->showMainPage("admincp/setting_system");
    }

    public function contractModule() {
        if (POST && AJAX) {
            $content = htmlspecialchars(str_replace("\r",'',trim($_POST['contract'])));
            foreach (explode("\n",$content) as $type) {
                if (mb_strlen($type,'UTF-8') > 15) $this->outputJson(-1,"[{$type}]超出了15字符的限制");
            }

            $_ENV['db']->runSql("REPLACE INTO `setting` SET `k`='contract',`v`='{$content}'");
            class_cache::delete('config');
            $this->outputJson(1,'操作成功');
        }

        $this->view->contract = $_ENV['config']['contract'];
        $this->showMainPage('admincp/setting_contract');
    }

    public function navigationModule() {
        if (POST && AJAX) {
            $data = htmlspecialchars(str_replace("\r",'',trim($_POST['navigation'])));
            $_ENV['db']->runSql("REPLACE INTO `setting` ".$this->buildSql(array(
                    'k' => 'snavi',
                    'v' => $data,
                )));
            class_cache::delete('config');
            $this->outputJson(1,'网站导航参数配置成功');
        }

        $this->view->navigation = $_ENV['config']['snavi'];
        $this->showMainPage("admincp/setting_navigation");
    }

	public function dailyModule() {
		if (POST && AJAX) {
			$project_phase = htmlspecialchars(str_replace("\r",'',trim($_POST['data']['project_phase'])));
			$work_result = htmlspecialchars(str_replace("\r",'',trim($_POST['data']['work_result'])));
            $i = htmlspecialchars(str_replace("\r",'',trim($_POST['data']['item'])));

			foreach (explode("\n",$project_phase) as $item) {
				if (mb_strlen($item,'UTF-8') > 20)
					$this->outputJson(-1,"{$item}超出了20字符限制");
			}

			foreach (explode("\n",$work_result) as $item) {
				if (mb_strlen($item,'UTF-8') > 20)
					$this->outputJson(-1,"{$item}超出了20字符限制");
			}

			foreach (explode("\n",$i) as $item) {
				if (mb_strlen($item,'UTF-8') > 20)
					$this->outputJson(-1,"{$item}超出了20字符限制");
			}

			$_ENV['db']->runSql("REPLACE INTO `setting` ".$this->buildSql(array(
					'k' => 'project_phase',
					'v' => $project_phase,
				)));
			$_ENV['db']->runSql("REPLACE INTO `setting` ".$this->buildSql(array(
					'k' => 'work_result',
					'v' => $work_result,
				)));
            $_ENV['db']->runSql("REPLACE INTO `setting` ".$this->buildSql(array(
                    'k' => 'daily_item',
                    'v' => $i,
                )));

			class_cache::delete('config');
			$this->outputJson(1,'操作成功');
		}

		$this->view->project_phase = $_ENV['config']['project_phase'];
		$this->view->work_result = $_ENV['config']['work_result'];
        $this->view->items = $_ENV['config']['daily_item'];
		$this->showMainPage('admincp/setting_daily');
	}
	
	public function cacheModule()
	{
		if (POST && AJAX) 
		{
			if($_POST['clear']=='all')
			{
				class_cache::delete('article_class');
				class_cache::delete('config');
				class_cache::delete('product_class');
			}
			if($_POST['article_class']=='1')
			{
				class_cache::delete('article_class');
			}
			if($_POST['config']=='1')
			{
				class_cache::delete('config');
			}
			if($_POST['product_class']=='1')
			{
				class_cache::delete('product_class');
			}
			$this->outputJson(1,'清除成功');
		}
		$this->showMainPage('admincp/setting_cache');
	}
}